﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace GMusicBoxUpdater
{
    public partial class frmMain : Form
    {
        public frmMain(string dst, string version)
        {
            InitializeComponent();

            try
            {
                XmlDocument xd = new XmlDocument(); xd.Load("http://www.mycreat.com/gmusic/test/update.xml");
                XmlNode xroot = xd.SelectSingleNode("root");
                bool isTesting = Convert.ToBoolean(xroot.SelectSingleNode("isTesting").InnerText);
#if !DEBUG
                if (isTesting) this.Close();
#endif

                lblUpdatedVersion.Text = xroot.SelectSingleNode("ver").InnerText;
                lblUpdateTime.Text = xroot.SelectSingleNode("time").InnerText;

                XmlNode xUpdates = xroot.SelectSingleNode("updates");
                XmlNodeList xAdd = xUpdates.SelectNodes("add/item");
                XmlNodeList xChange = xUpdates.SelectNodes("change/item");
                XmlNodeList xDelete = xUpdates.SelectNodes("delete/item");

                foreach (XmlNode xn in xAdd)
                    update.Add(new UpdateItem { Type = UpdateType.Add, Url = xn.InnerText.Trim() });
                foreach (XmlNode xn in xChange)
                {
                    UpdateItem ui = new UpdateItem { Type = UpdateType.Change };
                    ui.Url = xn.SelectSingleNode("url").InnerText.Trim();
                    ui.IsMainProgram = xn.SelectSingleNode("type").InnerText.Trim() == "MainProgram";
                    update.Add(ui);
                }

                foreach(XmlNode xn in xDelete)
                    update.Add(new UpdateItem { Type = UpdateType.Delete, Url = xn.InnerText.Trim() });

                XmlNode xDescribe = xroot.SelectSingleNode("describe");
                if (xDescribe == null)
                    label6.Text += "（暂无更新内容）";
                else
                {
                    XmlNodeList xDescription = xDescribe.SelectNodes("item");
                    for (int i = 0; i < xDescription.Count; i++)
                        label6.Text += "\n    " + (i + 1).ToString() + "、" + xDescription[i].InnerText;

                    this.Height += xDescription.Count * 16;
                }

                this.dst = dst;

                try
                {
                    string[] ver = version.Trim().Split('.');
                    string[] newver = lblUpdatedVersion.Text.Trim().Split('.');
                    if (ver.Length != 4 || newver.Length != 4) throw new Exception();

                    bool newer = false;
                    for (int i = 0; i < 4; i++)
                    {
                        if (int.Parse(newver[i]) > int.Parse(ver[i]))
                            newer = true;
                    }

                    if (!newer) this.Close();
                }
                catch
                {
                    if (version == lblUpdatedVersion.Text.Trim()) this.Close();
                }
            }
            catch
            {
                this.Close();
            }
        }

        List<UpdateItem> update = new List<UpdateItem>();
        string dst = "";

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void btnUpdateNow_Click(object sender, EventArgs e)
        {
            this.TopMost = false;
            frmDownloading fd = new frmDownloading(update.ToArray(), dst);
            fd.ShowDialog();
            this.Close();
        }
    }
}
